const pageConf = require('../config/config').page_conf,
  indexModel = require('../models/index'),
  extention = require('../lib/extention');

//类方式管理
//将路由里回调函数抽象到controller目录里管理
class IndexController {

  async index(ctx, next) {

    // await ctx.render('index', {
    //   title: 'Hello Koa 2',
    //   pageConf: pageConf.index
    // })

    const data = await indexModel.getXiaomiDatas({
      swiper: true,
      phone: true,
      field: true
    });

    console.log(data);

    const swiperData = data.swiper_data,
      phoneData = data.phone_data,
      fieldData = data.field_data;

    await ctx.render('index', {
      pageConf: pageConf.index,
      swiperData,
      phoneData,
      fieldData
    })

  }

  async list(ctx, next) {

    const keyword = ctx.params.keyword;

    const data = await indexModel.getXiaomiDatas({
      swiper: false,
      phone: true,
      field: true
    });

    const phoneData = data.phone_data,
      searchData = !keyword ? phoneData : extention.filterData(phoneData, keyword),
      fieldData = data.field_data;

    await ctx.render('list', {
      pageConf: pageConf.list,
      phoneData,
      searchData,
      fieldData
    });
  }

  async detail(ctx, next) {
    const id = ctx.params.id;

    const data = await indexModel.getXiaomiDatas({
      swiper: false,
      phone: true,
      field: true
    });

    const phoneData = data.phone_data,
      fieldData = data.field_data,
      phoneDetail = phoneData.filter(item => item.id === id)[0];

    await ctx.render('detail', {
      pageConf: pageConf.detail,
      phoneData,
      fieldData,
      phoneDetail
    });
  }

  async my404(ctx, next) {

    const data = await indexModel.getXiaomiDatas({
      swiper: false,
      phone: true,
      field: true
    });

    const phoneData = data.phone_data,
      fieldData = data.field_data;

    await ctx.render('404', {
      title: '404',
      pageConf: pageConf.my404,
      phoneData,
      fieldData
    });
  }
}

//导出实例
module.exports = new IndexController();